﻿using System;
using System.Globalization;
using Avalonia.Data.Converters;

namespace MusicFluent.Converters;

public class TimeConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is double d)
        {
            var i = (int)d;
            var s = "";
            while (true)
            {
                if (string.IsNullOrEmpty(s))
                {
                    var r = i % 60;
                    s = r < 10 ? $"0{r}" : r.ToString();
                }
                else
                {
                    s = $"{i % 60}:" + s;
                }

                i /= 60;
                if (i < 1)
                    break;
            }

            return !s.Contains(':') ? $"0:{s}" : s;
        }

        return value;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return value;
    }
}